﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FrienDev.Data;
using System.IO;
using System.Data;

namespace FrienDev.BusinessRules
{
    public class PhotoRules
    {
        private static PhotoRules _instance;

        public static PhotoRules Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new PhotoRules();
                }
                return _instance;
            }
        }

        private FrienDevDataContext m_DataContext = FrienDevDataContext.Instance;

        private PhotoRules()
        {
        }

        /// <summary>
        /// 得到活动的照片路径--小图
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public string GetActivitySmallPhotoUrl(int activityId, string filename)
        {
            return "/upload/ActivityPhoto/" + activityId + "/s/" + filename;
        }

        /// <summary>
        /// 得到活动的照片路径--原图
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public string GetActivityBackPhotoUrl(int activityId, string filename)
        {
            return "/upload/ActivityPhoto/" + activityId + "/back/" + filename;
        }

        /// <summary>
        /// 得到活动的照片路径--中图width=420px
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public string GetActivityMiddlePhotoUrl(int activityId, string filename)
        {
            return "/upload/ActivityPhoto/" + activityId + "/m/" + filename;
        }

        /// <summary>
        /// 得到活动的照片最新的10张
        /// </summary>
        /// <param name="activityId"></param>
        /// <returns></returns>
        public IEnumerable<Photo> GetActivityPhotosFor10(int activityId)
        {
            IEnumerable<Photo> photos = (from pho in m_DataContext.Photos
                                         where pho.ActivityId == activityId
                                         orderby pho.PhotoId descending
                                         select pho).Take(10);
            return photos;
        }

        /// <summary>
        /// 得到所有活动的照片
        /// </summary>
        /// <param name="activityId"></param>
        /// <returns></returns>
        public IEnumerable<Photo> GetActivityPhotos(int activityId,int beginId, int pageSize)
        {
            IEnumerable<Photo> photos = (from pho in m_DataContext.Photos
                                        where pho.ActivityId == activityId
                                        orderby pho.PhotoId descending
                                        select pho).Skip(beginId).Take(pageSize);
            return photos;
        }

        /// <summary>
        /// 删除活动的照片
        /// </summary>
        /// <param name="activityid"></param>
        /// <param name="parentid"></param>
        public void DeleteActivityPhoto(int activityId, int photoId)
        {
            IEnumerable<Photo> photos = from pho in m_DataContext.Photos
                                      where pho.PhotoId==photoId && pho.ActivityId==activityId
                                      select pho;
            if (photos.Count() > 0)
            {
                Photo photo = photos.SingleOrDefault();
                m_DataContext.Photos.DeleteOnSubmit(photo);
                m_DataContext.SubmitChanges();
            }
        }

        /// <summary>
        /// 得到所有活动的照片总数
        /// </summary>
        /// <param name="activityId"></param>
        /// <returns></returns>
        public int GetActivityPhotosCount(int activityId)
        {
            IEnumerable<Photo> photos = from pho in m_DataContext.Photos
                                        where pho.ActivityId == activityId
                                        orderby pho.PhotoId descending
                                        select pho;
            return photos.Count();
        }

        /// <summary>
        /// 插入活动照片
        /// </summary>
        /// <param name="activityId"></param>
        /// <returns></returns>
        public void InsertActivityPhoto(int activityId,string fileName,string description,string userId)
        {
            Photo photo = new Photo
            {
                ActivityId=activityId,
                FileName=fileName,
                Description = description,
                UserId=userId,
                UploadDateTime=DateTime.Now
            };
            m_DataContext.Photos.InsertOnSubmit(photo);
            m_DataContext.SubmitChanges();
        }

        /// <summary>
        /// 取得最新的活动照片
        /// </summary>
        /// <param name="activityId"></param>
        /// <returns></returns>
        public int GetActivityLastestPhotoId(int activityId)
        {
            IEnumerable<Photo> photos = from pho in m_DataContext.Photos
                                        where pho.ActivityId == activityId
                                        orderby pho.PhotoId descending
                                        select pho;
            if (photos.Count() == 0)
            {
                return 0;
            }
            return photos.First().PhotoId;
        }

        /// <summary>
        /// 根据PhotoId得到照片信息
        /// </summary>
        /// <param name="imageid"></param>
        /// <returns></returns>
        public Photo GetPhotoInfoById(int imageid)
        {
            IEnumerable<Photo> photos = from pho in m_DataContext.Photos
                                        where pho.PhotoId==imageid
                                        select pho;
            if (photos.Count() > 0)
            {
                return photos.SingleOrDefault();
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 更新相片描述
        /// </summary>
        /// <param name="imageid"></param>
        /// <param name="description"></param>
        public void UpdatePhotoDescription(int imageid,string description)
        {
            IEnumerable<Photo> photos = from pho in m_DataContext.Photos
                                        where pho.PhotoId == imageid
                                        select pho;
            Photo photo = photos.SingleOrDefault();
            photo.Description = description;
            m_DataContext.SubmitChanges();
        }

        /// <summary>
        /// 得到当前图片为所有活动的照片中的第几张
        /// </summary>
        /// <param name="activityId"></param>
        /// <returns></returns>
        public DataTable GetPhotosIndex(int activityId, int photoId)
        {
            IEnumerable<Photo> photos = from pho in m_DataContext.Photos
                                        where pho.ActivityId == activityId
                                        orderby pho.PhotoId descending
                                        select pho;
            DataTable dt = new DataTable();
            DataColumn dc1=new DataColumn("CurIndex",Type.GetType("System.Int32"));
            DataColumn dc2 = new DataColumn("Count", Type.GetType("System.Int32"));
            DataColumn dc3 = new DataColumn("PrePhotoId", Type.GetType("System.Int32"));
            DataColumn dc4 = new DataColumn("NextPhotoId", Type.GetType("System.Int32"));
            DataColumn dc5 = new DataColumn("Description", Type.GetType("System.String"));
            dt.Columns.Add(dc1);
            dt.Columns.Add(dc2);
            dt.Columns.Add(dc3);
            dt.Columns.Add(dc4);
            dt.Columns.Add(dc5);
            DataRow dr = dt.NewRow();
            int i = 1;
            Photo curPhoto = new Photo();
            foreach (Photo photo in photos)
            {
                if (photo.PhotoId == photoId)
                {
                    dr["CurIndex"]=i;
                    curPhoto = photo;
                    break;
                }
                i++;
            }
            if (photos.Count() == 1)
            {
                dr["PrePhotoId"] = 0;
                dr["NextPhotoId"] = 0;
            }
            else if(photos.Count() > 1)
            {
                if (i == 1)
                {
                    dr["PrePhotoId"] = photos.LastOrDefault().PhotoId;
                    dr["NextPhotoId"] = photos.ToArray().ElementAt(i).PhotoId;
                }
                else if (i == photos.Count())
                {
                    dr["PrePhotoId"] = photos.ToArray().ElementAt(i - 2).PhotoId;
                    dr["NextPhotoId"] = photos.FirstOrDefault().PhotoId;
                }
                else
                {
                    dr["PrePhotoId"] = photos.ToArray().ElementAt(i - 2).PhotoId;
                    dr["NextPhotoId"] = photos.ToArray().ElementAt(i).PhotoId;
                }
            }
            dr["Count"] = photos.Count();
            dr["Description"] = curPhoto.Description;
            dt.Rows.Add(dr);
            return dt;
        }
    }
}
